- /* sdfddadd.cpp by K.Tsuru */
- // function ID = 321 DRADIX
- /********************************************
- SDouble class
- m + n, same sign
- Provides an addition of two absolute values.
- It must consider the case such as
- 0.0000 abcd efgh .... wxyz ( = m)
- + 0.0000 .... 0000 ABCD EFGH .... WXYZ( = n).
- ********************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- static const char* func = "DDAdd";
-
- SDouble DDAdd(const SDouble& m, const SDouble& n){
- if((m.Type() != m.REAL)||(n.Type() != n.REAL)) m.SetError(m.RADIX_ERR, func, 321);
- if(m.Sign(321) == 0) return n;
- if(n.Sign(321) == 0) return m;
- //same sign
- if( m.Sign() != n.Sign() ) m.SetError(m.SYNTAX_ERR, func, 321);
- //check m >> n || m << n
- //"long" type is necessary in the 16 bits system.
- long di = (long)m.NetRdxExp() - (long)n.NetRdxExp(); //difference in exponent
- long max_fig = (long)m.CurrentMaxSize() -1;
- if( labs(di) >= max_fig) return di > 0 ? m : n;
-
- int de = m.rdxExp - n.rdxExp;
- //When de < 0, exchange m <--> n.
- if(de < 0) return DDAdd(n, m);
-
- const fType* Mf = m.ReadFigures();
- const fType* Nf;
- SDouble result, N; // radix = DRADIX only
- uint ml = m.Last(), mf = m.First(), nl, nf;// ml < CurrentMaxSize()
- int top, btm;
-
- // step 1 : make the exponents the same value
- if(!de){ //same exponent
- Nf = n.ReadFigures(); nl = n.Last(); nf = n.First();
- } else {
- N = n;
- N.ShiftArray(de);//moving figures, size is extended if necessary
- N.rdxExp += de;
- if(N.Sign(321) == 0) return m; //check n << m again
- Nf = N.ReadFigures(); nl = N.Last(); nf = N.First();
- }
-
- uint rsize = max(ml, nl) +1u;
- result.valloc(rsize , -1); //allocate memory, not initialize
- result.figure.clear(rsize); //initialize lower region
- fType* rf = result.figure.Elements();
- // step 2 : copy lower part of longer number to that of result
- if(ml < nl){ // n is longer
- memcpy(rf+ml+1u, Nf+ml+1u, (nl-ml)*sizeof(fType));
- } else if(ml > nl){
- memcpy(rf+nl+1u, Mf+nl+1u, (ml-nl)*sizeof(fType));
- }
- // step 3 : addition of the part in which both figures are non-zero
- register int i;
- fType w = 0;
- top = (int)max(mf, nf);
- btm = (int)min(ml, nl);
- #ifndef NDEBUG
- result.figure(btm); // error check
- assert(top > 0);
- #endif
- for(i = btm; i >= top; i--){
- w += Mf[i] + Nf[i];
- rf[i] = w % DRADIX;
- w /= DRADIX;
- }
- // step 4 : upper region of m and/or n is filled by zero
- top = (int)min(mf, nf);
- int end = 0; //addition has ended?
- if(mf != nf){
- Mf = (mf < nf) ? Mf : Nf;
- for(;w && (i >= top); i--){ //while carry is not clear
- w += Mf[i];
- rf[i] = w % DRADIX;
- w /= DRADIX;
- }
- //copy residual figures
- if(w == 0){
- memcpy(rf, Mf, (i+1)*sizeof(fType));
- end = 1; i = 0;
- }
- }
- // step 5 : disposal of carry to highest figure
- if(w){
- for(; i >= top; i--){ //normalize only, a few times
- rf[i] = w % DRADIX;
- w /= DRADIX;
- }
- }
- if(!end) result.figure[i] = w; //carry, including w = 0
- if(i) result.figure.clear(0, uint(i-1));//fill upper region with zero
- //Get the position.
- result.aTail = w ? (uint)i : (uint)top;
-
- register uint h;
- h = max(ml, nl); // != last
- while(!rf[h]) h--;
- result.aHead = h;
- //Set sign and exponent.
- result.SetSign(m.Sign()); result.rdxExp = m.rdxExp;
- result.Reform(321); //clear the carry to figure[0], etc.
- return result;
- }
sdfddadd.cpp : last modifiled at 2017/03/13 14:31:58(3,388 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).